package com.ruoyi.sysem.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.sysem.domain.BorrowUser;
import com.ruoyi.sysem.domain.CreditUser;
import com.ruoyi.sysem.domain.UserCredit;
import com.ruoyi.sysem.service.ICreditUserService;
import com.ruoyi.sysem.service.IIdentityUserService;
import com.ruoyi.sysem.service.UserService;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 借款用户授信资料Controller
 * 
 * @author ruoyi
 * @date 2022-12-13
 */
@RestController
@RequestMapping("/user")
public class CreditUserController extends BaseController
{
    @Autowired
    private ICreditUserService creditUserService;
    @Autowired
    private IIdentityUserService identityUserService;
    @Autowired
    KieSession kieSession;
    /**
     * 查询借款用户授信资料列表
     */
    @RequiresPermissions("system:user:list")
    @GetMapping("/list")
    public TableDataInfo list(CreditUser creditUser)
    {
        startPage();
        List<CreditUser> list = creditUserService.selectCreditUserList(creditUser);
        return getDataTable(list);
    }

    /**
     * 导出借款用户授信资料列表
     */
    @RequiresPermissions("system:user:export")
    @Log(title = "借款用户授信资料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CreditUser creditUser)
    {
        List<CreditUser> list = creditUserService.selectCreditUserList(creditUser);
        ExcelUtil<CreditUser> util = new ExcelUtil<CreditUser>(CreditUser.class);
        util.exportExcel(response, list, "借款用户授信资料数据");
    }

    /**
     * 获取借款用户授信资料详细信息
     */
    @RequiresPermissions("system:user:query")
    @GetMapping(value = "query/{cid}")
    public AjaxResult getInfo(@PathVariable("cid") Long cid)
    {
        return AjaxResult.success(creditUserService.selectCreditUserByCid(cid));
    }

    /**
     * 新增借款用户授信资料
     */
    @RequiresPermissions("system:user:add")
    @Log(title = "借款用户授信资料", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody CreditUser creditUser)
    {
        return toAjax(creditUserService.insertCreditUser(creditUser));
    }

    /**
     * 修改借款用户授信资料
     */
    @RequiresPermissions("system:user:edit")
    @Log(title = "借款用户授信资料", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody CreditUser creditUser)
    {
        return toAjax(creditUserService.updateCreditUser(creditUser));
    }

    /**
     * 删除借款用户授信资料
     */
    @RequiresPermissions("system:user:remove")
    @Log(title = "借款用户授信资料", businessType = BusinessType.DELETE)
	@DeleteMapping("remove/{cids}")
    public AjaxResult remove(@PathVariable Long[] cids)
    {
        return toAjax(creditUserService.deleteCreditUserByCids(cids));
    }

    @Autowired
    private UserService userService;
    /**
     * 用户可以获得的信用额度
     * @param cid
     * @return
     */
    @PostMapping("audit")
    public AjaxResult audit(@RequestBody UserCredit userCredit){
    CreditUser creditUser = creditUserService.selectCreditUserByCid(Long.parseLong(userCredit.getCid().toString()));
    userCredit.setCredit(creditUser.getCredit());
    userCredit.setDegree(userCredit.getDid());
    kieSession.insert(userCredit);
        //执行规则引擎
    kieSession.fireAllRules();
    kieSession.dispose();
    BorrowUser borrowUser = new BorrowUser();
    borrowUser.setUid(creditUser.getUid());
    borrowUser.setBorrowPrice(Double.parseDouble(userCredit.getCreditScore().toString()));
    borrowUser.setResiduePrice(Double.parseDouble(userCredit.getCreditScore().toString()));
    userService.updateUser(borrowUser);
    return AjaxResult.success("您最总可贷款"+userCredit.getCreditScore()+"元");
}

}
